package hw;



import util.AtmApi;
import util.Util;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Login extends JPanel implements ActionListener {
    private Atm atm;
    private Icon ldBtnImg = Util.getIcon("/buttonLand.jpg");
    private Icon loginimg = Util.getIcon("/buttonLogin.jpg");
    private JButton landBtn = new JButton(ldBtnImg);
    private JButton loginBtn = new JButton(loginimg);
    private JTextField useNameText = new JTextField();
    private JPasswordField passwordText = new JPasswordField();
    public int count =0;

    public Login(Atm atm) {
        this.atm = atm;
 // #############################     配置组件位置      ################################
        useNameText.setBounds(400, 400, 280, 50);
        passwordText.setBounds(400, 500, 280, 50);
        landBtn.setBounds(700, 400, 140, 50);
        loginBtn.setBounds(700, 500, 140, 50);
        landBtn.addActionListener(this);


//#############################         添加组件      ################################
        this.add(useNameText);
        this.add(passwordText);
        this.add(landBtn);
        this.add(loginBtn);
        this.setLayout(null);
        this.setOpaque(false);
    }
    //#############################         登录函数      ################################

    public  void surepassword (){
        if (count==3){
            JOptionPane.showConfirmDialog(this,"系统已被锁请联系管理员");
            System.exit(0);
        }
        boolean status =false;
        AtmApi atmApi =new AtmApi();
        atm.setUser(atmApi.getByNameAndPwd(useNameText.getText(),passwordText.getText()));
//        for (int i = 0;i<atm.getUseList().size();i++){
//            if (atm.getUseList().get(i).getName().equals(useNameText.getText())&&atm.getUseList().get(i).getPassword().equals(passwordText.getText())){
//
//               atm.setIndex(i) ;
//               atm.setUser(atm.getUseList().get(i));
//                status = true;
//            }
//        }
        if (atm.getUser() != null){
            atm.switchPanel("menu");
            status = true;
        }

        if (!status){
            count++;
            System.out.println("还有"+(3-count)+"次");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == landBtn) {
            surepassword();
        }
    }


}
